<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');
require_once(dirname(__FILE__) . '/pinyin.php');


function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    if (curl_errno($curl)) {
        echo 'Errno' . curl_error($curl);//捕抓异常
    }
    curl_close($curl);

    return $res;
}

//研高

for ($i = 1; $i < 54; $i++) {

    $url = "https://sgf.zishaq.com/web/index.php?store_id=1&r=api/default/goods-list&cat_id=31&page=" . $i . "&access_token=X4UKUQEO6ZtN1T2ZvkymOG1ZR6oDCBOD&_version=2.8.9&_platform=wx";

    $res = httpGet($url);
    $arr = json_decode($res, true);
    $list = $arr['data']['list'];
    $time = time();

    foreach ($list as $key => $val) {

        $title_int = $val['name'];
        echo $title_int . "<br>";
        if ($title_int != "") {
            $t_arr = explode(" ", $title_int);
            $array = array_diff($t_arr, ["", " "]);
            $array = array_column($array, null);

            $id_init = $val['id'];


            if (count($array) == 3) {


                $title = $array[0];
                $dashi = trim($array[1]);
                $niliao = trim($array[2]);
                if ($niliao == "底槽青") {
                    $niliao = "底槽清";
                }
                if ($niliao == "天清泥") {
                    $niliao = "天青泥";
                }
                if ($dashi == "汪 叶") {
                    $dashi = "汪叶";
                }
                $tbid = $val['id'];

                echo $niliao . "<br>";
                echo $dashi . "<br>";


                $url2 = 'https://sgf.zishaq.com/web/index.php?store_id=1&r=api/default/goods&id=' . $id_init . '&access_token=X4UKUQEO6ZtN1T2ZvkymOG1ZR6oDCBOD&_version=2.8.9&_platform=wx';

                $res2 = httpGet($url2);
                $arr_detail = json_decode($res2, true);

                $detail = $arr_detail['data'];

                $canshu = $detail['goods_canshu'];
                $desc = $detail['goods_brief'];
                $price = $detail['max_price'];
                $content = $detail['detail'];



                $channel_id = 0;
                $channid_arr = $dosql->GetOne("select `id` from fa_cms_channel where `name`='$niliao'  ");
                if (is_array($channid_arr) && isset($channid_arr['id'])) {
                    $channel_id = $channid_arr['id'];
                } else {
                    echo "********************" . $niliao . "****************************<br>";

                    $orderid = 0;
                    $max = $dosql->GetOne("select max(`weigh`) as wmax from fa_cms_channel where model_id=3 ");
                    if (is_array($max) && isset($max['wmax'])) {
                        $orderid = $max['wmax'] + 1;
                    }

                    $diyname = Pinyin::get($niliao);

                    //插入 呢料 栏目
                    $dosql->Execute("insert into `fa_cms_channel`(`type`,model_id,parent_id,`name`,diyname,weigh,listtpl,showtpl,pagesize,createtime,updatetime,status) values ('list','3','8','$niliao','$diyname','$orderid','list_product.html','show_product.html','12','$time','$time','normal')");
                }


                //不存在大师，插入大师

                $dashi_arr = $dosql->GetOne("select `id` from fa_cms_archives where title='$dashi' and model_id=2 ");
                if (is_array($dashi_arr) && isset($dashi_arr['id'])) {
                    $aid = $dashi_arr['id'];
                } else {
                    echo "==================" . $dashi . "===========================<br>";

                    $dorderid = 0;
                    $dmax = $dosql->GetOne("select max(`weigh`) as wmax from fa_cms_archives where 1 ");
                    if (is_array($dmax) && isset($dmax['wmax'])) {
                        $dorderid = $dmax['wmax'] + 1;
                    }

                    //插入  名家
                    $dosql->Execute("insert into `fa_cms_archives` (`channel_id`,model_id,title,weigh,createtime,updatetime,publishtime,status) values ('2','2','$dashi','$dorderid','$time','$time','$time','normal')");
                    $lastid = $dosql->GetLastID();
                    $dosql->ExecNoneQuery("insert into `fa_cms_addonmingjia`(`id`,`content`) values ('$lastid','')");
                }


                //插入 产品
                $dosql->Execute("insert into `fa_cms_archives`(channel_id,model_id,title,image,createtime,updatetime,publishtime,status,tbid) values ('$channel_id','3','$title','','$time','$time','$time','normal','$id_init')");
                $lastid_id = $dosql->GetLastID();
                $dosql->ExecNoneQuery("insert into `fa_cms_addonproduct`(`id`,`content`,`productprice`,`aid`,`canshu`) values ('$lastid_id','$content','$price','$aid','$canshu')");


                echo $title . "添加成功" . "<br>";

            }


        }
        //var_dump($t_arr);

        //$channid=$dosql->GetOne("select `id` from fa_cms_channel where name='' ");


        /* $dosql->Execute("insert into `fa_cms_archives`(channel_id,model_id,title,image,createtime,updatetime,publishtime,status,tbid) values ('$channel_id','2','$title','','$time','$time','$time','normal','$tbid')");
         $lastid = $dosql->GetLastID();
         $dosql->ExecNoneQuery("insert into `fa_cms_addonmingjia`(`id`,`content`) values ('$lastid','$content')");


         echo $title . "添加成功" . "<br>";*/


    }

}
/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    $filename_arr = parse_url($url);
    $path = $filename_arr['path'];
    $filename_arr = explode('/', $path);
    $filename = array_pop($filename_arr);

    $upload_url = '/uploads/image/news/';
    $upload_dir = PHPMYWIND_UPLOAD . "/images/news/";

    //上传文件路径
    $save_url = $upload_url . $filename;
    $save_dir = $upload_dir . $filename;
    echo "保存路径" . $save_dir . "<br/>";

    if (file_exists($save_dir)) {
        return $save_url;
    }


    //引入下载类
    require_once(PHPMYWIND_DATA . '/httpfile/down.class.php');

    //初始化变量
    $htd = new HttpDown();
    $htd->OpenUrl($url);


    //判断文件类型
    $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
    if (!in_array($htd->GetHead("content-type"), $sparr)) {
        return FALSE;
    } else {
        $type = $htd->GetHead("content-type");

        if ($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if ($type == 'image/png')
            $tempfile_ext = 'png';

        else if ($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        echo "保存";

        $rs = $htd->SaveToBin($save_dir);
    }

    $htd->Close();
    return ($rs ? $save_url : '');
}


?>